package com.study.zgw.nio;


import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class HelloOne {
    public static void main(String[] args) {
        //3
        ByteBuffer source = ByteBuffer.allocate(32);
        source.put("hello,world\nI'm zhangsan\nHo".getBytes(StandardCharsets.UTF_8));
        split(source);
        source.put("w are you?\n".getBytes(StandardCharsets.UTF_8));
        split(source);
    }

    private static void split(ByteBuffer source) {
        //切换成读模式
        source.flip();
        for (int i=0;i<source.limit();i++){
            if (source.get(i) == '\n'){
                int length = i+1 - source.position();
                ByteBuffer target = ByteBuffer.allocate(length);
                for (int j=0;j<length;j++){
                    target.put(source.get());
                }
                String ss = new String(target.array(), StandardCharsets.UTF_8);
                String str = Charset.defaultCharset().decode(target).toString();
                System.out.println("========>  "+ss);
            }
        }

        source.compact();
    }
}
